﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;

namespace InvExport
{
    public static class DBUtil
    {
        private static SQLiteConnection _conn;
        private static string _connStr;
        public static string DBPath;

        #region 获取当前打开的数据库

        public static string GetCurrentPath()
        {
            try
            {
                if (_conn is null)
                    return null;
                if (_conn.State == ConnectionState.Broken | _conn.State == ConnectionState.Closed)
                    return null;
                return _conn.FileName;
            }
            catch (Exception)
            {
                return null;
            }
        }

        #endregion 获取当前打开的数据库

        #region 打开/关闭数据库

        private static void connDB()
        {
            try
            {
                if (string.IsNullOrEmpty(_connStr))
                    _connStr = "Data Source=" + DBPath + ";Pooling=true; Password=LoveR1314";
                if (null == _conn)
                {
                    _connStr = "Data Source=" + DBPath + ";Pooling=true; Password=LoveR1314";
                    _conn = new SQLiteConnection(_connStr);
                }
                if (_conn.State == ConnectionState.Closed | _conn.State == ConnectionState.Broken)
                    _conn.Open();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static void CloseDB()
        {
            try
            {
                if (_conn != null)
                {
                    _conn.Close();
                    _conn.Dispose();
                    _conn = null;
                }
            }
            catch (Exception)
            {
            }
        }

        #endregion 打开/关闭数据库

        #region 查询数据库

        public static DataTable Query(string sqlstr)
        {
            try
            {
                if (string.IsNullOrEmpty(sqlstr))
                    throw new Exception("指令不能为空！");
                connDB();
                SQLiteDataAdapter da = new SQLiteDataAdapter(sqlstr, _conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (null == dt)
                    throw new Exception("未知错误！");
                return dt;
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion 查询数据库

        #region 执行命令

        public static void Exec(string sqlstr)
        {
            try
            {
                if (string.IsNullOrEmpty(sqlstr))
                    throw new Exception("命令不能为空！");
                if (sqlstr.ToLower().Contains("truncate"))
                    throw new Exception("不能执行清空命令");
                connDB();
                SQLiteCommand cmd = new SQLiteCommand(sqlstr, _conn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion 执行命令
    }
}